package com.feng.store.service.impl;

import com.feng.store.entity.User;
import com.feng.store.mapper.UserMapper;
import com.feng.store.service.IUserService;
import com.feng.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void reg(User user) {
      //调用mapper中findByUsername判断用户名是否被注册
        User result = userMapper.findByUsername(user.getUsername());


        //判断result是否为null，不为null，则抛出用户名被占用的异常
        if(result!=null){
            throw new UsernameDuplicatdException("用户名已存在！");
        }
        //密码加密处理的实现：md5加密
        String oldPassword=user.getPassword();
        //获得盐
        String salt=UUID.randomUUID().toString().toUpperCase();
        //将密码和盐作为一个整体进行加密处理
        String newpassword = getMd5Password(oldPassword, salt);
        user.setPassword(newpassword);
        user.setSalt(salt);

        //补全数据
        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        user.setCreatedTime(new Date());
        user.setModifiedTime(new Date());
        //如果为null，可以注册
        Integer rows = userMapper.insertUser(user);
        if(rows!=1){
            throw new InsertException("不好意思,网络出现了一点点波动~");
        }

    }

    @Override
    public User login(String username, String password) {
        //根据用户名称查询用户数据是否存在，如果不存在则抛出异常
        User result = userMapper.findByUsername(username);
        if(result==null){
            throw new UserNotFoundExcption("用户名不存在！");
        }
        //检测用户的密码是否匹配
        //1.先获取到数据库中加密之后的密码
        String oldpassword = result.getPassword();
        //2.和用户传递过来的密码进行比较
        //2.1 获取用户所对应的盐值
        String salt=result.getSalt();
        //2.2 将用户的密码按照相同的md5算法进行加密
        String newMd5Password=getMd5Password(password,salt);
        //将两个密码进行比较
        if(!oldpassword.equals(newMd5Password)){
            throw new PasswordNotMatchException("用户密码错误！");
        }
        //判断is_delete字段的值是否为1，为1表示被标记的字段已被用户注销
        if(result.getIsDelete()==1){
            throw new UserNotFoundExcption("用户名不存在");
        }
        return result;

    }

    @Override
    public void changpassword(Integer uid, String username, String oldpassword, String newpassword) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundExcption("用户不存在！");
        }
        //原始密码与数据库密码的比较
        String salt = result.getSalt();
        String md5Password = getMd5Password(oldpassword, salt);
        if(!result.getPassword().equals(md5Password)){
            throw new PasswordNotMatchException("原始密码错误！");

        }
        //将新密码设置到数据库中
        String newmd5Password = getMd5Password(newpassword, salt);
        Integer rows = userMapper.updatePassword(uid, newmd5Password, username, new Date());
        if(rows!=1){
            throw new UpdateException("更新数据时产生了未知的异常！");
        }

    }

    @Override
    public User getByUid(Integer uid) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundExcption("用户不存在！");


        }
        return result;
    }

    /**
     *
     * @param uid 用户的id
     * @param username 用户的名字
     * @param user 前端传过来的用户信息
     */
    @Override
    public void changgeInfo(Integer uid, String username, User user) {

        user.setUsername(username);
        user.setUid(uid);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows = userMapper.updateInfoByUid(user);
        if(rows!=1){
            throw new UpdateException("更新时数据产生了未知的异常！");
        }


    }

    @Override
    public void changgeAvatar(String avatar, Integer uid, String username) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
          throw new UserNotFoundExcption("用户不存在！");
        }
        Integer rows = userMapper.updateAvatarByUid(uid, avatar, username, new Date());
        if(rows!=1){
            throw new UpdateException("更新头像产生了未知的异常！");
        }
    }

    /**
     * md5算法加密处理的方法
     */
    private String getMd5Password(String password,String salt){
        //md5加密算法的调用(进行三次加密)
        for(int i=0;i<3;i++){
           password= DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
        }
        //返回加密之后的密码
        return password;

    }
}
